﻿namespace NMortgage.UI.Model
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;

    public sealed class ExtraPayment : ABaseValidatedType<ExtraPayment, ExtraPaymentValidator>
    {
        #region Fields

        private const int MONTHS_IN_YEAR = 12;

        private double amount;
        private ExtraPaymentFrequency extraPaymentFrequency;
        private ExtraPaymentType extraPaymentType = ExtraPaymentType.OneTime;
        private int frequency;
        private int months;
        private int offsetMonths=0;
        private int offsetYears=0;
        private int years;

        #endregion Fields

        #region Constructors

        public ExtraPayment()
        {
        }

        #endregion Constructors

        #region Properties

        public double Amount
        {
            get
            {
                return amount;
            }
            set
            {
                amount = value;
                NotifyOfPropertyChange(() => Amount);
            }
        }

        public string Description
        {
            get
            {
                if (Type == ExtraPaymentType.OneTime)
                {
                    return string.Format(CultureInfo.CurrentCulture, Properties.Resources.ExtraPaymentDescriptionFormat,
                        Amount, TotalOffsetInMonths, Properties.Resources.MonthsLabel, Frequency);
                }
                else
                {
                    return string.Format(CultureInfo.CurrentCulture, Properties.Resources.ExtraPaymentRecurringDescriptionFormat,
                        Amount, Frequency, FrequencyMeasure, TotalOffsetInMonths, TotalOffsetInMonths+TotalDurationInMonths);
                }
            }
        }

        public int Frequency
        {
            get
            {
                return frequency;
            }
            set
            {
                frequency = value;
                NotifyOfPropertyChange(() => Frequency);
            }
        }

        public ExtraPaymentFrequency FrequencyMeasure
        {
            get
            {
                return extraPaymentFrequency;
            }
            set
            {
                extraPaymentFrequency = value;
                NotifyOfPropertyChange(() => FrequencyMeasure);
                NotifyOfPropertyChange(() => Frequency);
            }
        }
        
        public bool IsRecurring
        {
            get { return Type != ExtraPaymentType.OneTime; }
        }

        public int Months
        {
            get
            {
                return months;
            }
            set
            {
                months = value;
                NotifyOfPropertyChange(() => Years);
                NotifyOfPropertyChange(() => Months);
            }
        }

        public int OffsetMonths
        {
            get
            {
                return offsetMonths;
            }
            set
            {
                offsetMonths = value;
                NotifyOfPropertyChange(() => OffsetYears);
                NotifyOfPropertyChange(() => OffsetMonths);
            }
        }

        public int OffsetYears
        {
            get
            {
                return offsetYears;
            }
            set
            {
                offsetYears = value;
                NotifyOfPropertyChange(() => OffsetYears);
                NotifyOfPropertyChange(() => OffsetMonths);
            }
        }

        public int TotalDurationInMonths
        {
            get { return (Years * MONTHS_IN_YEAR) + Months; }
        }

        public int TotalOffsetInMonths
        {
            get { return (OffsetYears * MONTHS_IN_YEAR) + OffsetMonths; }
        }

        public ExtraPaymentType Type
        {
            get
            {
                return extraPaymentType;
            }
            set
            {
                extraPaymentType = value;
                NotifyOfPropertyChange(() => Frequency);
                NotifyOfPropertyChange(() => FrequencyMeasure);
                NotifyOfPropertyChange(() => Months);
                NotifyOfPropertyChange(() => Type);
                NotifyOfPropertyChange(() => Years);
            }
        }

        public int Years
        {
            get
            {
                return years;
            }
            set
            {
                years = value;
                NotifyOfPropertyChange(() => Years);
                NotifyOfPropertyChange(() => Months);
            }
        }

        #endregion Properties
    }
}